<!-- components/insights/SmartInsights.vue -->
<template>
  <div class="smart-insights" v-if="insights.length > 0">
    <el-card class="insights-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <el-icon><Lightning /></el-icon>
          <span>智能洞察</span>
        </div>
      </template>
      <div class="insights-content">
        <div 
          v-for="insight in insights" 
          :key="insight.id"
          class="insight-item"
          :class="`insight-${insight.type}`"
          @click="handleInsightClick(insight)"
        >
          <div class="insight-icon">
            <el-icon :size="16" :color="getInsightColor(insight.type)">
              <component :is="getInsightIcon(insight.type)" />
            </el-icon>
          </div>
          <div class="insight-text">
            <div class="insight-title">{{ insight.title }}</div>
            <div class="insight-description">{{ insight.description }}</div>
          </div>
          <div class="insight-priority" :class="`priority-${insight.priority}`">
            {{ getPriorityText(insight.priority) }}
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { Lightning, Warning, Opportunity, DataAnalysis } from '@element-plus/icons-vue'

interface Insight {
  id: string
  type: 'opportunity' | 'risk' | 'analysis' | 'recommendation'
  title: string
  description: string
  priority: 'low' | 'medium' | 'high'
  action?: () => void
}

const props = defineProps<{
  insights: Insight[]
}>()

const emit = defineEmits<{
  (e: 'action', insight: Insight): void
}>()

const getInsightIcon = (type: string) => {
  switch (type) {
    case 'opportunity': return Opportunity
    case 'risk': return Warning
    case 'analysis': return DataAnalysis
    case 'recommendation': return Lightning
    default: return Lightning
  }
}

const getInsightColor = (type: string) => {
  switch (type) {
    case 'opportunity': return '#52c41a'
    case 'risk': return '#fa8c16'
    case 'analysis': return '#1890ff'
    case 'recommendation': return '#722ed1'
    default: return '#1890ff'
  }
}

const getPriorityText = (priority: string) => {
  switch (priority) {
    case 'high': return '高优先级'
    case 'medium': return '中优先级'
    case 'low': return '低优先级'
    default: return ''
  }
}

const handleInsightClick = (insight: Insight) => {
  if (insight.action) {
    insight.action()
  }
  emit('action', insight)
}
</script>

<style scoped>
.smart-insights {
  margin-bottom: 20px;
}

.insights-card {
  border: 1px solid #ebeef5;
  border-radius: 8px;
}

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: #1f2d3d;
}

.insights-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.insight-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid transparent;
}

.insight-item:hover {
  background-color: #f5f7fa;
  border-color: #dcdfe6;
}

.insight-opportunity {
  border-left: 4px solid #52c41a;
}

.insight-risk {
  border-left: 4px solid #fa8c16;
}

.insight-analysis {
  border-left: 4px solid #1890ff;
}

.insight-recommendation {
  border-left: 4px solid #722ed1;
}

.insight-icon {
  flex-shrink: 0;
}

.insight-text {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.insight-title {
  font-weight: 500;
  color: #1f2d3d;
}

.insight-description {
  font-size: 14px;
  color: #606266;
}

.insight-priority {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 4px;
  font-weight: 500;
}

.priority-high {
  background-color: #fff1f0;
  color: #f56c6c;
}

.priority-medium {
  background-color: #fff7e6;
  color: #faad14;
}

.priority-low {
  background-color: #f0f9ff;
  color: #1890ff;
}
</style>